import 'package:flutter/material.dart';
import 'package:flutter_09_game/config/history_route_ovserver.dart';

import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/pages/home/home_logic.dart';
import 'package:flutter_09_game/pages/main/main_logic.dart';
import 'package:flutter_09_game/pages/profile/profile_logic.dart';
import 'package:flutter_09_game/utils/log/utils_log.dart';

import 'package:flutter_easyloading/flutter_easyloading.dart';

import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:flutter_09_game/widgets/keyboard/tap_hide_keyboard.dart';
import 'package:flutter_localizations/flutter_localizations.dart';

import 'application.dart';
import 'config/app_config.dart';
import 'config/app_routes.dart';
import 'config/app_theme.dart';
import 'config/app_language.dart';

void main() async {
  WidgetsBinding _ = WidgetsFlutterBinding.ensureInitialized();

  // 设置后台消息处理函数
  // FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

  await Application().initialize();
  await AppConfig.setup();

  // 初始化语言管理器
  // LanguageService.init();

  // 使用默认路由，将显示欢迎页面
  runApp(MyApp(
    title: AppConfig.appName,
  ));
}

class MyApp extends StatelessWidget {
  final String title;
  final bool useDefaultNavigation;

  const MyApp(
      {super.key, required this.title, this.useDefaultNavigation = true});

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 812),
      builder: (context, child) {
        return GetMaterialApp(
          title: title,
          debugShowCheckedModeBanner: false,
          transitionDuration: const Duration(milliseconds: 210),
          theme: AppTheme.setImaginifyTheme(),
          initialRoute: AppRoutes.splash,
          getPages: AppRoutes.pages,

          // 多语言配置
          // translations: AppTranslation(),是
          translationsKeys: AppTranslation.translations,
          locale: AppLanguage.defaultLocale,
          supportedLocales: AppLanguage.supportedLocales,
          fallbackLocale: AppLanguage.getFallbackLocale(),
          localizationsDelegates: const [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            GlobalCupertinoLocalizations.delegate,
          ],
          navigatorObservers: [
            // AppRouteObserver().routeObserver,
            routeHistoryObserver,
            // if (Application.canGoogleService)
            //   FirebaseAnalyticsObserver(
            //     analytics: FirebaseAnalytics.instance,
            //     // nameExtractor: routeNameFromSetting
            //   ),
          ],
          routingCallback: (routing) {
            // routing.
            // routing.args
            if (routing?.current == AppRoutes.main && routing?.isBack == true) {
              if (Get.isRegistered<HomeLogic>() &&
                  Get.isRegistered<MainLogic>() &&
                  Get.find<MainLogic>().currentIndex.value == 0) {
                // Get.find<HomeLogic>().loadNoticeData();
                Get.find<HomeLogic>().beginPoolAnimated();
              } else if (Get.isRegistered<ProfileLogic>() &&
                  Get.isRegistered<MainLogic>() &&
                  Get.find<MainLogic>().currentIndex.value == 4) {
                // Get.find<HomeLogic>().loadNoticeData();
                Get.find<ProfileLogic>().beginWalletAnimated();
              }
              // Get.find<HomeLogic>().loadNoticeData();

              // Get.find<HomeLogic>().beginPoolAnimated();
              // 刷新逻辑放这里
            }
          },
          builder: (context, widget) {
            return MediaQuery(
              data: MediaQuery.of(context)
                  .copyWith(textScaler: const TextScaler.linear(1.0)),
              child: FlutterEasyLoading(
                child: TapHideKeyboard(child: widget!),
              ),
            );
          },
        );
      },
    );
  }
}
